/****************************************************************************/
/*This program runs regressions                                             */
/****************************************************************************/

#delimit;
capture log close;
set more off;

local pathsmr  C:\Research;
local Torpath  `pathsmr'\Tornadoes\0515Miles_CountyM;

/***************************************************************************/
/* Create a loop that loops through the damage levels                      */
/***************************************************************************/

local k = 2;
while `k' <= 4{;

if `k' == 1 {;
local damvar alltreat;
};

if `k' == 2 {;
local damif g1;
local damvar g1treat;
local damlabel Low;
};

if `k' == 3 {;
local damif g2;
local damvar g2treat;
local damlabel Medium;
}; 

if `k' == 4 {;
local damif g3;
local damvar g3treat;
local damlabel High;
}; 



/****************************************************************************/
/*Run Regressions                                                           */
/****************************************************************************/

if `k' == 1 {;
use `Torpath'\Temp\temp_program_5_`damvar'_reg_analysis.dta, clear;
};

if `k' > 1 {;
use `Torpath'\Temp\temp_program_5_`damvar'.dta, clear;
};

/****************************************************************************/
/*Do DD with hit as the treatment                                           */
/****************************************************************************/
capture drop balanced_dd*;

gen balanced_dd = 0;
replace balanced_dd = 1 if year >= ydate-3 & year <= ydate+3;

gen balanced_dd_2 = 0;
replace balanced_dd_2 = 1 if year >= ydate-4 & year <= ydate+4;


destring bgfips, replace;
gen Ei = ydate;
replace Ei = . if treated ==0;

local depvar "firm emp sales";



did_imputation  ln_firm_all bgfips year Ei if balanced_dd ==1 , pretrends(3) horizons(0/3) hbalance maxit(30000) tol(0.001);


event_plot, default_look graph_opt(xtitle("Years since the Tornado") legend(off)
	title("`damlabel' Damage") xlabel(-3(1)3) saving(C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\ln_firm_all_bjs_`damvar', replace));

did_imputation  ln_emp_all bgfips year Ei if balanced_dd ==1 , pretrends(3) horizons(0/3) hbalance maxit(30000) tol(0.001);

event_plot, default_look graph_opt(xtitle("Years since the Tornado") legend(off)
	title("`damlabel' Damage") xlabel(-3(1)3) saving(C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\ln_emp_all_bjs_`damvar', replace));


did_imputation  ln_sales_all bgfips year Ei if balanced_dd ==1 , pretrends(3) horizons(0/3) hbalance maxit(30000) tol(0.001);

event_plot, default_look graph_opt(xtitle("Years since the Tornado") legend(off)
	title("`damlabel' Damage") xlabel(-3(1)3) saving(C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\ln_sales_all_bjs_`damvar', replace));


clear;
local k = `k' + 1;
};


/****************************************************************************/
/*Put all DD the graphs together                                            */
/****************************************************************************/


cd "C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\";

graph combine ln_firm_all_bjs_g1treat.gph ln_firm_all_bjs_g2treat.gph ln_firm_all_bjs_g3treat.gph , col(3) row(1) ysize(17) xsize(20) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) title("Panel A: Business Survival") saving(C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\combine1, replace);

graph combine ln_emp_all_bjs_g1treat.gph ln_emp_all_bjs_g2treat.gph ln_emp_all_bjs_g3treat.gph , col(3) row(1) ysize(17) xsize(20) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) title("Panel B: Employees") saving(C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\combine2, replace) ;

graph combine ln_sales_all_bjs_g1treat.gph ln_sales_all_bjs_g2treat.gph ln_sales_all_bjs_g3treat.gph , col(3) row(1) ysize(17) xsize(20) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) title("Panel C: Sales") saving(C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\combine3, replace) ;


graph combine combine1.gph combine2.gph combine3.gph , col(1) row(3) ysize(17) xsize(20) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) ;

